﻿
(function () {
    appModule.controller('common.views.lawsRegulationses.createOrEditModal', [
        '$scope', '$uibModalInstance', 'abp.services.app.lawsRegulations', 'lawsRegulationsId', 'textAngularManager', 'abp.services.app.file', 'FileUploader',
        function ($scope, $uibModalInstance, lawsRegulationsService, lawsRegulationsId, textAngularManager, fileService, fileUploader) {
            var vm = this;
            vm.saving = false;
            //首先将lawsRegulations数据设置为null
            vm.lawsRegulations = null;

            //上传方法
            vm.uploader = new fileUploader({
                url: abp.appPath + 'Profile/UploadFile',
                headers: {
                    "X-XSRF-TOKEN": abp.security.antiForgery.getToken()
                },
                queueLimit: 1,
                autoUpload: true,
                removeAfterUpload: true,
                filters: [{
                    name: 'imageFilter',
                    fn: function (item, options) {
                        //File type check
                        var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
                        if ('|jpg|jpeg|png|gif|'.indexOf(type) === -1) {
                            abp.message.warn(app.localize('Picture_Warn_SizeLimitTen'));
                            return false;
                        }

                        //File size check
                        if (item.size > 10485760) //10MB
                        {
                            abp.message.warn(app.localize('Picture_Warn_SizeLimitTen'));
                            return false;
                        }

                        return true;
                    }
                }]
            });

            vm.uploader.onSuccessItem = function (fileItem, response, status, headers) {
                if (response.success) {
                    vm.fileTitle = response.result.fileName;
                    vm.fileUrl = response.result.filePath;
                    vm.lawsRegulations.fileID = response.result.fileID;

                } else {
                    abp.message.error(response.error.message);
                }
            };

            //文件下载方法
            vm.downFile = function (filePath) {
                window.location.target = '_blank';
                window.location.href = filePath;
            }

            vm.fileClick = function () {
                $("#FileID").click();
            };



            //触发保存方法
            vm.save = function () {
                vm.saving = true;
                lawsRegulationsService.createOrUpdateLawsRegulationsAsync({ lawsRegulationsEditDto: vm.lawsRegulations }).then(function () {
                    abp.notify.info(app.localize('SavedSuccessfully'));
                    $uibModalInstance.close();
                }).finally(function () {
                    vm.saving = false;
                });


            };
            //取消关闭页面
            vm.cancel = function () {
                $uibModalInstance.dismiss();
            };

            //初始化页面
            function init() {
                lawsRegulationsService.getLawsRegulationsForEditAsync({
                    id: lawsRegulationsId
                }).then(function (result) {
                    vm.lawsRegulations = result.data.lawsRegulations;

                    if (vm.lawsRegulations.fileID != 0) {
                        //绑定文件数据
                        fileService.getFileForEditAsync({
                            id: vm.lawsRegulations.fileID
                        }).then(function (result) {
                            vm.fileTitle = result.data.file.title;
                            vm.fileUrl = result.data.file.url;
                        });
                    }
                });
            }
            //执行初始化方法
            init();
        }
    ]);
})();